package com.hwz.hwzliving.commodity.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.Query;

import com.hwz.hwzliving.commodity.dao.CategoryDao;
import com.hwz.hwzliving.commodity.entity.CategoryEntity;
import com.hwz.hwzliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }



    //返回树形分类列表
    @Override
    public List<CategoryEntity> listTree() {
        //获取所有的分类信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //流式计算
        List<CategoryEntity> collect = categoryEntities.stream().filter(categoryEntity -> {
            //过滤出第一层级数据
            return categoryEntity.getParentId() == 0;
        }).map(categoryEntity -> {
            //设置子分类
            categoryEntity.setChildCategories(getTreeChildCategories(categoryEntity, categoryEntities));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            //进行排序
            return category1.getSort() - category2.getSort();
        }).collect(Collectors.toList());
        return collect;
    }

    //获取categoryId对应的层级关系
    @Override
    public List<Long> getSelectCategoryId(Long categoryId) {
        List<CategoryEntity> categoryEntities = this.list();
        List<Long> ints = new ArrayList<>();
        ints.add(categoryId);
//        获取当前分类对象
        CategoryEntity category = new CategoryEntity();
        for (CategoryEntity categoryEntity : categoryEntities) {
            if (categoryEntity.getId().longValue() == categoryId.longValue()){
                category = categoryEntity;
            }
        }
        //分配父对象并加入到数组
        while (category.getParentId() != 0){
            for (CategoryEntity categoryEntity : categoryEntities) {
                if (categoryEntity.getId().longValue() == category.getParentId().longValue()){
                    category = categoryEntity;
                    ints.add(category.getId());
                }
            }
        }
        Collections.reverse(ints);


        return ints;
    }

    public List<CategoryEntity> getTreeChildCategories(CategoryEntity root, List<CategoryEntity> all){
        //流式计算 获取到子分类数据
        List<CategoryEntity> collect = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId().equals(root.getId());
        }).map(categoryEntity -> {
            //给子分类设置数据
            categoryEntity.setChildCategories(getTreeChildCategories(categoryEntity, all));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            //进行排序
            return category1.getSort() - category2.getSort();
        }).collect(Collectors.toList());

        return collect;
    }

}